/**
 * ManualClassifyController.java created by zhangzhidong 
 * at 下午7:12:37 2016年4月18日
 */
package controllers.wechatlab;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import pojo.weichatlab.Label;
import pojo.weichatlab.Wechat;
import pojo.weichatlab.WechatClassMap;
import service.wechatlab.LabelService;
import service.wechatlab.WechatClassMapService;
import service.wechatlab.WechatService;
/**
 * @author zhangzhidong
 *
 */
@Controller
@RequestMapping("wechatlab")
public class ManualClassifyController extends WechatBaseController {
	@Autowired
	WechatService ws;
	@Autowired LabelService ls;
	@Autowired WechatClassMapService wcms;
	@RequestMapping("/re.do")
	public ModelAndView rednerMain(HttpServletRequest req,HttpServletResponse res){
			String id = req.getParameter("id");
			if(id==null){
				id = "1";
			}
			List<Label> list = ls.selectAllParentLabels();
			List<Wechat> wechat = ws.selectWechatById(id);
			
			if(wechat.size()!=0){
				return renderView("main", "classes",list).addObject("wechat", wechat.get(0));
			}else{
				return renderView("main", "classes",list);
			}
	}
	
	@RequestMapping("/getAllChildren.do")
	@ResponseBody
	public String ajaxLoadAllChildClasses(HttpServletRequest req,HttpServletResponse res){
		List<Label> childList = ls.selectAllChildLabels();
		return jsonDump(childList);	
	}
	@RequestMapping("/getChildren.do")
	@ResponseBody
	public String ajaxLoadChildClasses(HttpServletRequest req,HttpServletResponse res){
			String id = req.getParameter("id");
			if(id==null){
				return jsonDump(ParamsError);
			}else{
				int labelId = 0;
				try{
					labelId = Integer.parseInt(id);
					List<Label> list = ls.fetchChildLables(labelId);
					return jsonDump(list);
				}catch(Exception e){
					return jsonDump(ParamsError);
				}
			}	
	}
	@RequestMapping("/get.do")
	@ResponseBody
	public String ajaxLoadWechat(HttpServletRequest req,HttpServletResponse res){
			String id = req.getParameter("id");
			if(id==null){
				return jsonDump(ParamsError);
			}else{
				List<Wechat> list = ws.selectWechatById(id);
				return jsonDump(list);
			}	
	}
	@RequestMapping("/update.do")
	@ResponseBody
	public String ajaxUpdateWechat(HttpServletRequest req,HttpServletResponse res){
			String id =req.getParameter("id");
			if(id==null){
				return jsonDump(ParamsError);
			}else{
				String labels[] = req.getParameterValues("labelid");
				if(labels!=null){
					Wechat wt = ws.selectWechatById(id).get(0);
					if(wt==null){
						return jsonDump(ParamsError); 
					}
					int iid = Integer.parseInt(id);
					wt.setId(iid);
					List<WechatClassMap> l = wt.getWechatclassmaps();
					//这个map表示<classid ,location>对于一个wechat对象实例的wechatclassmaps，类标为classid的位置在location
					Map<Integer,Integer> m = new HashMap<Integer, Integer>();
					int size = l.size();
					for(int i = 0 ;i<size;i++){
						WechatClassMap wcm = l.get(i);
						m.put(wcm.getClassid(), i);
					}
					for(String labelid:labels){
						Integer loc = m.get(Integer.parseInt(labelid));
						if(loc==null){
							l.add(new WechatClassMap(iid,Integer.parseInt(labelid),1,new Date()));
						}else{
							WechatClassMap wcm = l.get(loc);
							wcm.inc();
						}
					}
					ws.saveOrUpdateWechat(wt);					
					return jsonDump(SUCCESS);
				}else{
					return jsonDump(ParamsError);
				}
			}		
	}
}
